import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Components from 'unplugin-vue-components/vite'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    // 自动按需引入elementPlus和指定的自定义组件，注意导出的.d.ts文件需要在tsconfig.json中配置进去
    Components({
      // resolvers: [ElementPlusResolver()],
      dirs: ['src/common-ui/*/', 'src/components/*/'],
      extensions: ['vue'],
      deep: false,
      dts: path.resolve(__dirname, 'src/types/auto-components.d.ts')
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    // 解决开发环境跨域问题
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    port: 9004, //端口
    https: false, //是否为https协议
    host: '0.0.0.0', //主机地址
    open: false, //项目启动完后是否在浏览器中自动打开
    //本地代理配置
    proxy: {
      '/myapi': {
        target: 'http://192.168.2.11:8089', // 线上测试地址
        // target: 'http://192.168.2.65:8080', // 胡家梦测试地址
        changeOrigin: true, // 如果接口跨域，需要进行这个参数配置
        rewrite: (path) => path.replace(/^\/myapi/, '')
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 全局scss变量
        additionalData: '@use "@/assets/style/variable.scss" as *;'
      }
    }
  }
})
